package com.example.webservcicetest;

import java.io.IOException;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import android.os.Bundle;
import android.app.Activity;
import android.widget.EditText;
import android.widget.Button; 
import android.util.Log;  
import android.view.View; 

import com.google.resting.*;
import com.google.resting.component.impl.ServiceResponse;

public class MainActivity extends Activity {
	
	//String URL = "http://www.google.com";  
    //String result = "";  
    //String deviceId = "xxxxx" ;   
    //final String tag = "Your Logcat tag: "; 

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	
	
	 final EditText txtSearch = (EditText)findViewById(R.id.txtSearch);  
     txtSearch.setOnClickListener(new EditText.OnClickListener(){  
         public void onClick(View v){txtSearch.setText("");}  
     });  
       
     final Button btnSearch = (Button)findViewById(R.id.btnSearch);  
     btnSearch.setOnClickListener(new Button.OnClickListener(){  
         public void onClick(View v) {  
             String query = txtSearch.getText().toString();  
             try {
				callWebService(query);
			} catch (ParserConfigurationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SAXException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
         }  
     });  
	}

	public class Map
	{
		public String name;
		public String lat;
		public String lon;
	}
	
	/*@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}*/

    public void callWebService(String q) throws ParserConfigurationException, SAXException, IOException
    { 
    	ServiceResponse response=Resting.get("http://www.serveravip-test.appspot.com/ServerAvip",80);
    	String xml;
    	
    	//System.out.println(response); //Print the response.
    	
    	///Le fichier Xml en string
    	xml=response.getResponseString();
    	
    	//System.out.print(xml);
    	Log.e("xml", xml);
    	
    	//String xml = "<?xml ...";
    	//xml = xml.trim().replaceFirst("^([\\W]+)<","<");
    	////Parser le fichier xml
    	
    	/****************PARSER AVEC DOM****************************/   	
    	
    	DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    	DocumentBuilder db = dbf.newDocumentBuilder();
    	Document doc = db.parse(new InputSource(new StringReader(xml)));
    	
    	// normalize the document
    	doc.getDocumentElement().normalize();
    	// get the root node
    	for(int j=0; j<2;j++)
    	{
    		Map map= new Map();
	    	NodeList nodeList = doc.getElementsByTagName("map");
	    	Node node=nodeList.item(j);
	    	// the  node has three child nodes
	    	for (int i = 0; i < node.getChildNodes().getLength(); i++) 
	    	{
		    	Node temp=node.getChildNodes().item(i);
		    	if(temp.getNodeName().equalsIgnoreCase("name")){
		    		map.name=temp.getTextContent();
		    	}
		    	else if(temp.getNodeName().equalsIgnoreCase("lat")){
		    		map.lat=temp.getTextContent();
		    	}
		    	else if(temp.getNodeName().equalsIgnoreCase("lon")){
		    		map.lon=temp.getTextContent();
		    	}
	    	}
	    	
	    	Log.e("map","map: " + map.name+ " lat: "+map.lat+" lon: "+map.lon);
    	}
    	
      /***************************************************************/
   
	  /*  HttpClient httpclient = new DefaultHttpClient();  
	    HttpGet request = new HttpGet(URL + q);  
	    request.addHeader("deviceId", deviceId);  
	    ResponseHandler<String> handler = new BasicResponseHandler();  
	    try {  
	        result = httpclient.execute(request, handler);  
	    } catch (ClientProtocolException e) {  
	        e.printStackTrace();  
	    } catch (IOException e) {  
	        e.printStackTrace();  
	    }  
	    httpclient.getConnectionManager().shutdown();   */
	  //  Log.i(tag, result);  
	} // end callWebService()  
	
	
}
